package com.travelms.service.commonuserpage;

import com.travelms.pojo.Sale;
import com.travelms.dao.DBFind;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class CancelOrder {
    private DBFind dbFind = new DBFind();
    public CancelOrder(String username) throws SQLException, IOException {
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        int current = 0;
        stack.push(current);


        while (true) {
            if (current == 0) {
                System.out.println("1.一键退掉全部的票");
                System.out.println("2.退出");
            }
            while (true) {
                System.out.println("亲输入:");
                String s = sc.nextLine();
                if (!DataEffect.isNumber(s)) {//输入效验
                    System.out.println("输入错误");
                } else if (!(s.compareTo("0") > 0 && s.compareTo("3") < 0)) {
                    System.out.println("输入错误");
                } else {
                    current = Integer.parseInt(s);//记录输入的层级
                    stack.push(current);//压入栈
                    break;
                }
            }
            if (current == 2) {
                //为3则退出
                break;
            }else if(current == 1){
                ArrayList<Sale> sales = dbFind.getALLSale(username);
                if (sales == null || sales.isEmpty()){
                    System.out.println("您没有任何订购信息哦");
                    System.out.println("按任意键退出");
                    String i = sc.nextLine();

                }else {
                    for (Sale sale : sales) {
                        System.out.println("您的订购信息如下");
                        System.out.println(sale);

                    }
                    if (dbFind.cancelTotalOrder(username)){
                        System.out.println("退票成功");
                        System.out.println("按任意键退出");
                        String i = sc.nextLine();
                    }else {
                        System.out.println("退票失败");
                        System.out.println("按任意键退出");
                        String i = sc.nextLine();
                    }
                }


                stack.pop();
                current = stack.peek();
            }
        }
    }
}